{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0.向量组的线性相关性\n",
    "## 0.1.环境、库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import itertools\n",
    "from functools import reduce\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体   \n",
    "plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题\n",
    "x, y, z, t = sp.symbols('x y z t')\n",
    "k, m, n = sp.symbols('k m n', integer=True)\n",
    "f, g, h = sp.symbols('f g h', cls=sp.Function)\n",
    "sp.init_printing()\n",
    "# common tool function\n",
    "def getM(var_name,r,c):\n",
    "    \"\"\"\n",
    "    make a sympy s matrix\n",
    "    :param var_name:the symbol name\n",
    "    :param r:matrix s rows\n",
    "    :param c:matrix s cols\n",
    "    \"\"\"\n",
    "    if r>1 and c>1:\n",
    "        ss=sp.symbols(\"{0}_{{{{0:{1}}}{{0:{2}}}}}\".format(var_name,r,c))        \n",
    "    elif r==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,c))\n",
    "    elif c==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,r))\n",
    "    else:\n",
    "        raise Exception('invalid input parameter！')\n",
    "    return sp.Matrix([ss[i*c:(i+1)*c] for i in range(r)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "table {width:95%;}\n",
       "table td, table th, table tr {text-align:left !important;border: 1px  #888888 solid !important;}\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%HTML\n",
    "<style>\n",
    "table {width:95%;}\n",
    "table td, table th, table tr {text-align:left !important;border: 1px  #888888 solid !important;}\n",
    "</style>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.向量组及其线性组合\n",
    "**定义:**向量是指一个具有大小和方向，且满足平行四边形法则的集合对象。一般来说，同时满足具有大小和方向两个性质的集合对象就可以称为向量。与向量相对的叫做标量，标量和向量最大的区别就是是否有方向。向量是线性代数中的基本概念，也是机器学习的基础数据表示形式。例如计算机阅读文本的过程首先就会将文本分词，然后用向量表示。这是因为向量很适合在高维空间中表达和处理。在机器学习中会接触到的诸如投影、降维的概念，都是在向量的基础上做的。\n",
    "\n",
    "在$\\mathbb{R}^n$空间中定义的向量$\\vec V$，可以用一个包含 n 个实数的有序集来表示，即$\\vec V=\\begin{bmatrix}v_1\\\\\\vdots\\\\v_n\\end{bmatrix}$，这个有序集里的每个元素称为向量的 分量 。例如向量$(2,1)或<2,1>,\\begin{bmatrix}2\\\\1\\end{bmatrix}\\in \\mathbb{R}^n$。\n",
    ">* 分量全为实数的向量称为**实向量**.\n",
    "* 分量全为复数的向量称为**复向量**.\n",
    "\n",
    "**注:**\n",
    ">* 这里一般只讨论实向量(特别说明除外).\n",
    "* 行向量和列向量总被看作是两个不同的向量.\n",
    "* 这里讨论的向量一般当作列向量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**若干个同维数的列向量(行向量)所组成的集合称为**向量组**.当$R(A)\\leq n$时,齐次线性方程组$Ax=0$的全体解组成的向量组含有无穷多个向量.含有有限个向量的有序向量组与矩阵一一对应."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}}\\\\a_{{1}{0}}\\\\a_{{2}{0}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{1}}\\\\a_{{1}{1}}\\\\a_{{2}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{2}}\\\\a_{{1}{2}}\\\\a_{{2}{2}}\\end{matrix}\\right]\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤  ⎡⎡a_{{0}{0}}⎤  ⎡a_{{0}{1}}⎤  ⎡a_{{0}{2}\n",
       "⎜⎢                                  ⎥  ⎢⎢          ⎥  ⎢          ⎥  ⎢         \n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥, ⎢⎢a_{{1}{0}}⎥  ⎢a_{{1}{1}}⎥  ⎢a_{{1}{2}\n",
       "⎜⎢                                  ⎥  ⎢⎢          ⎥  ⎢          ⎥  ⎢         \n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦  ⎣⎣a_{{2}{0}}⎦  ⎣a_{{2}{1}}⎦  ⎣a_{{2}{2}\n",
       "\n",
       "}⎤⎤⎞\n",
       " ⎥⎥⎟\n",
       "}⎥⎥⎟\n",
       " ⎥⎥⎟\n",
       "}⎦⎦⎠"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xa=getM('a',xn,xn)\n",
    "xas=sp.Matrix([[xa[:,i]] for i in range(xn)]).T#向量组形式\n",
    "xa,xas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**给定向量组$A:a_1,\\dots,a_m,对于任何一组实数k_1,\\dots,k_m,表达式:k_1a_1,\\dots,k_ma_m$,称为向量组$A$的一个**线性组合**.$k_1,\\dots,k_m$称为这个**线性组合的系数**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}k_{0}\\\\k_{1}\\\\k_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡k_{0}⎤\n",
       "⎢     ⎥\n",
       "⎢k_{1}⎥\n",
       "⎢     ⎥\n",
       "⎣k_{2}⎦"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 线性组合的系数\n",
    "xk=getM('k',xn,1)\n",
    "xk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} k_{0} + a_{{0}{1}} k_{1} + a_{{0}{2}} k_{2}\\\\a_{{1}{0}} k_{0} + a_{{1}{1}} k_{1} + a_{{1}{2}} k_{2}\\\\a_{{2}{0}} k_{0} + a_{{2}{1}} k_{1} + a_{{2}{2}} k_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅k_{0} + a_{{0}{1}}⋅k_{1} + a_{{0}{2}}⋅k_{2}⎤⎤\n",
       "⎢⎢                                                      ⎥⎥\n",
       "⎢⎢a_{{1}{0}}⋅k_{0} + a_{{1}{1}}⋅k_{1} + a_{{1}{2}}⋅k_{2}⎥⎥\n",
       "⎢⎢                                                      ⎥⎥\n",
       "⎣⎣a_{{2}{0}}⋅k_{0} + a_{{2}{1}}⋅k_{1} + a_{{2}{2}}⋅k_{2}⎦⎦"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向量组的一个线性组合\n",
    "xas*xk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**给定向量组$A:a_1,\\dots,a_m和向量b,如果存在一组实数k_1,\\dots,k_m,使得b=k_1a_1,\\dots,k_ma_m$,则向量$b$是向量组$A$的线性组合,这时称**向量$b$能由向量组$A$的线性表示**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} k_{0} + a_{{0}{1}} k_{1} + a_{{0}{2}} k_{2}\\\\a_{{1}{0}} k_{0} + a_{{1}{1}} k_{1} + a_{{1}{2}} k_{2}\\\\a_{{2}{0}} k_{0} + a_{{2}{1}} k_{1} + a_{{2}{2}} k_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅k_{0} + a_{{0}{1}}⋅k_{1} + a_{{0}{2}}⋅k_{2}⎤⎤\n",
       "⎢⎢                                                      ⎥⎥\n",
       "⎢⎢a_{{1}{0}}⋅k_{0} + a_{{1}{1}}⋅k_{1} + a_{{1}{2}}⋅k_{2}⎥⎥\n",
       "⎢⎢                                                      ⎥⎥\n",
       "⎣⎣a_{{2}{0}}⋅k_{0} + a_{{2}{1}}⋅k_{1} + a_{{2}{2}}⋅k_{2}⎦⎦"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向量b能由向量组A线性表示\n",
    "xb=xas*xk\n",
    "xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$n阶单位矩阵E_n的列向量叫做{\\color{red}{n维单位坐标向量}}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方程组有解?$\\Leftrightarrow$向量$b$是否能用向量组$A$线性表示?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡b_{0}⎤⎤\n",
       "⎢⎢     ⎥⎥\n",
       "⎢⎢b_{1}⎥⎥\n",
       "⎢⎢     ⎥⎥\n",
       "⎣⎣b_{2}⎦⎦"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xbs=sp.Matrix([[getM('b',xn,1),]])\n",
    "xbs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性方程组的向量组线性组合的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} k_{0} + a_{{0}{1}} k_{1} + a_{{0}{2}} k_{2}\\\\a_{{1}{0}} k_{0} + a_{{1}{1}} k_{1} + a_{{1}{2}} k_{2}\\\\a_{{2}{0}} k_{0} + a_{{2}{1}} k_{1} + a_{{2}{2}} k_{2}\\end{matrix}\\right]\\end{matrix}\\right] = \\left[\\begin{matrix}\\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅k_{0} + a_{{0}{1}}⋅k_{1} + a_{{0}{2}}⋅k_{2}⎤⎤   ⎡⎡b_{0}⎤⎤\n",
       "⎢⎢                                                      ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎢⎢a_{{1}{0}}⋅k_{0} + a_{{1}{1}}⋅k_{1} + a_{{1}{2}}⋅k_{2}⎥⎥ = ⎢⎢b_{1}⎥⎥\n",
       "⎢⎢                                                      ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎣⎣a_{{2}{0}}⋅k_{0} + a_{{2}{1}}⋅k_{1} + a_{{2}{2}}⋅k_{2}⎦⎦   ⎣⎣b_{2}⎦⎦"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Eq(xas*xk,xbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性方程组的一般形式=向量方程的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} k_{0} + a_{{0}{1}} k_{1} + a_{{0}{2}} k_{2}\\\\a_{{1}{0}} k_{0} + a_{{1}{1}} k_{1} + a_{{1}{2}} k_{2}\\\\a_{{2}{0}} k_{0} + a_{{2}{1}} k_{1} + a_{{2}{2}} k_{2}\\end{matrix}\\right] = \\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}⋅k_{0} + a_{{0}{1}}⋅k_{1} + a_{{0}{2}}⋅k_{2}⎤   ⎡b_{0}⎤\n",
       "⎢                                                      ⎥   ⎢     ⎥\n",
       "⎢a_{{1}{0}}⋅k_{0} + a_{{1}{1}}⋅k_{1} + a_{{1}{2}}⋅k_{2}⎥ = ⎢b_{1}⎥\n",
       "⎢                                                      ⎥   ⎢     ⎥\n",
       "⎣a_{{2}{0}}⋅k_{0} + a_{{2}{1}}⋅k_{1} + a_{{2}{2}}⋅k_{2}⎦   ⎣b_{2}⎦"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Eq(xa*xk,xbs[0,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**向量$b$能由向量组$A$线性表示$\\Leftrightarrow$线性方程组$Ax=b$有解$\\Leftrightarrow R(A)=R(A,b)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**设有向量组$A:a_1,\\dots,a_m及B:b_1,\\dots,b_l$,若向量组$B$中的每一个向量都能由向量组$A$线性表示,则称${\\color{red}{向量组B能由向量组A线性表示}}$.若向量组$A$与向量组$B$能互相线性表示,则称这两个${\\color{red}{向量组等价}}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}}\\\\a_{{1}{0}}\\\\a_{{2}{0}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{1}}\\\\a_{{1}{1}}\\\\a_{{2}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{2}}\\\\a_{{1}{2}}\\\\a_{{2}{2}}\\end{matrix}\\right]\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\left[\\begin{matrix}b_{{0}{0}}\\\\b_{{1}{0}}\\\\b_{{2}{0}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{0}{1}}\\\\b_{{1}{1}}\\\\b_{{2}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{0}{2}}\\\\b_{{1}{2}}\\\\b_{{2}{2}}\\end{matrix}\\right]\\end{matrix}\\right], \\quad \\left[\\begin{matrix}k_{{0}{0}} & k_{{0}{1}} & k_{{0}{2}}\\\\k_{{1}{0}} & k_{{1}{1}} & k_{{1}{2}}\\\\k_{{2}{0}} & k_{{2}{1}} & k_{{2}{2}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡⎡a_{{0}{0}}⎤  ⎡a_{{0}{1}}⎤  ⎡a_{{0}{2}}⎤⎤  ⎡⎡b_{{0}{0}}⎤  ⎡b_{{0}{1}}⎤  ⎡b_{\n",
       "⎜⎢⎢          ⎥  ⎢          ⎥  ⎢          ⎥⎥  ⎢⎢          ⎥  ⎢          ⎥  ⎢   \n",
       "⎜⎢⎢a_{{1}{0}}⎥  ⎢a_{{1}{1}}⎥  ⎢a_{{1}{2}}⎥⎥, ⎢⎢b_{{1}{0}}⎥  ⎢b_{{1}{1}}⎥  ⎢b_{\n",
       "⎜⎢⎢          ⎥  ⎢          ⎥  ⎢          ⎥⎥  ⎢⎢          ⎥  ⎢          ⎥  ⎢   \n",
       "⎝⎣⎣a_{{2}{0}}⎦  ⎣a_{{2}{1}}⎦  ⎣a_{{2}{2}}⎦⎦  ⎣⎣b_{{2}{0}}⎦  ⎣b_{{2}{1}}⎦  ⎣b_{\n",
       "\n",
       "{0}{2}}⎤⎤  ⎡k_{{0}{0}}  k_{{0}{1}}  k_{{0}{2}}⎤⎞\n",
       "       ⎥⎥  ⎢                                  ⎥⎟\n",
       "{1}{2}}⎥⎥, ⎢k_{{1}{0}}  k_{{1}{1}}  k_{{1}{2}}⎥⎟\n",
       "       ⎥⎥  ⎢                                  ⎥⎟\n",
       "{2}{2}}⎦⎦  ⎣k_{{2}{0}}  k_{{2}{1}}  k_{{2}{2}}⎦⎠"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xa,xb,xk=[getM(i,xn,xn) for i in 'abk']\n",
    "xas,xbs=[sp.Matrix([[j[:,i]] for i in range(xn)]).T for j in [xa,xb]]\n",
    "# xk为线性表示的系数矩阵\n",
    "xas,xbs,xk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} k_{{0}{0}} + a_{{0}{1}} k_{{1}{0}} + a_{{0}{2}} k_{{2}{0}}\\\\a_{{1}{0}} k_{{0}{0}} + a_{{1}{1}} k_{{1}{0}} + a_{{1}{2}} k_{{2}{0}}\\\\a_{{2}{0}} k_{{0}{0}} + a_{{2}{1}} k_{{1}{0}} + a_{{2}{2}} k_{{2}{0}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{0}} k_{{0}{1}} + a_{{0}{1}} k_{{1}{1}} + a_{{0}{2}} k_{{2}{1}}\\\\a_{{1}{0}} k_{{0}{1}} + a_{{1}{1}} k_{{1}{1}} + a_{{1}{2}} k_{{2}{1}}\\\\a_{{2}{0}} k_{{0}{1}} + a_{{2}{1}} k_{{1}{1}} + a_{{2}{2}} k_{{2}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}a_{{0}{0}} k_{{0}{2}} + a_{{0}{1}} k_{{1}{2}} + a_{{0}{2}} k_{{2}{2}}\\\\a_{{1}{0}} k_{{0}{2}} + a_{{1}{1}} k_{{1}{2}} + a_{{1}{2}} k_{{2}{2}}\\\\a_{{2}{0}} k_{{0}{2}} + a_{{2}{1}} k_{{1}{2}} + a_{{2}{2}} k_{{2}{2}}\\end{matrix}\\right]\\end{matrix}\\right] = \\left[\\begin{matrix}\\left[\\begin{matrix}b_{{0}{0}}\\\\b_{{1}{0}}\\\\b_{{2}{0}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{0}{1}}\\\\b_{{1}{1}}\\\\b_{{2}{1}}\\end{matrix}\\right] & \\left[\\begin{matrix}b_{{0}{2}}\\\\b_{{1}{2}}\\\\b_{{2}{2}}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅k_{{0}{0}} + a_{{0}{1}}⋅k_{{1}{0}} + a_{{0}{2}}⋅k_{{2}{0}}⎤  ⎡a_{\n",
       "⎢⎢                                                                     ⎥  ⎢   \n",
       "⎢⎢a_{{1}{0}}⋅k_{{0}{0}} + a_{{1}{1}}⋅k_{{1}{0}} + a_{{1}{2}}⋅k_{{2}{0}}⎥  ⎢a_{\n",
       "⎢⎢                                                                     ⎥  ⎢   \n",
       "⎣⎣a_{{2}{0}}⋅k_{{0}{0}} + a_{{2}{1}}⋅k_{{1}{0}} + a_{{2}{2}}⋅k_{{2}{0}}⎦  ⎣a_{\n",
       "\n",
       "{0}{0}}⋅k_{{0}{1}} + a_{{0}{1}}⋅k_{{1}{1}} + a_{{0}{2}}⋅k_{{2}{1}}⎤  ⎡a_{{0}{0\n",
       "                                                                  ⎥  ⎢        \n",
       "{1}{0}}⋅k_{{0}{1}} + a_{{1}{1}}⋅k_{{1}{1}} + a_{{1}{2}}⋅k_{{2}{1}}⎥  ⎢a_{{1}{0\n",
       "                                                                  ⎥  ⎢        \n",
       "{2}{0}}⋅k_{{0}{1}} + a_{{2}{1}}⋅k_{{1}{1}} + a_{{2}{2}}⋅k_{{2}{1}}⎦  ⎣a_{{2}{0\n",
       "\n",
       "}}⋅k_{{0}{2}} + a_{{0}{1}}⋅k_{{1}{2}} + a_{{0}{2}}⋅k_{{2}{2}}⎤⎤   ⎡⎡b_{{0}{0}}\n",
       "                                                             ⎥⎥   ⎢⎢          \n",
       "}}⋅k_{{0}{2}} + a_{{1}{1}}⋅k_{{1}{2}} + a_{{1}{2}}⋅k_{{2}{2}}⎥⎥ = ⎢⎢b_{{1}{0}}\n",
       "                                                             ⎥⎥   ⎢⎢          \n",
       "}}⋅k_{{0}{2}} + a_{{2}{1}}⋅k_{{1}{2}} + a_{{2}{2}}⋅k_{{2}{2}}⎦⎦   ⎣⎣b_{{2}{0}}\n",
       "\n",
       "⎤  ⎡b_{{0}{1}}⎤  ⎡b_{{0}{2}}⎤⎤\n",
       "⎥  ⎢          ⎥  ⎢          ⎥⎥\n",
       "⎥  ⎢b_{{1}{1}}⎥  ⎢b_{{1}{2}}⎥⎥\n",
       "⎥  ⎢          ⎥  ⎢          ⎥⎥\n",
       "⎦  ⎣b_{{2}{1}}⎦  ⎣b_{{2}{2}}⎦⎦"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Eq(xas*xk,xbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**说明:**矩阵$\\color{red}{B的列向量组}$能由矩阵$\\color{red}{A的列向量组}$线性表示,$\\color{red}{K}$为这一线性表示的系数矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$A是一个m\\times n矩阵,$\n",
    ">* 对$A$实行一次$\\color{red}{初等行变换}$,相当于在$\\color{red}{A的左边}$乘以相应的$m$阶初等矩阵;\n",
    "* 对$A$实行一次$\\color{red}{初等列变换}$,相当于在$\\color{red}{A的右边}$乘以相应的$n$阶初等矩阵;\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**若$C=AB$,那么\n",
    ">* 矩阵$\\color{red}{C的行向量组}$能由矩阵$\\color{red}{B的行向量组}$线性表示,$\\color{red}{A}$为这一线性表示的系数矩阵.($\\color{blue}{A在左边}$)\n",
    "* 矩阵$\\color{red}{C的列向量组}$能由矩阵$\\color{red}{A的列向量组}$线性表示,$\\color{red}{B}$为这一线性表示的系数矩阵.($\\color{blue}{B在右边}$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$A\\overset{{\\color{red}{c}}}{\\sim}B\\\\{\\color{blue}{\\Leftrightarrow}} A经过有限次初等{\\color{red}{列}}变换变成B\\\\\\Leftrightarrow 存在有限个初等矩阵P_1,\\dots,P_l,使AP_1\\dots P_l=B\\\\\\Leftrightarrow 存在m阶{\\color{red}{可逆矩阵}}P,使得AP=B(P可以看成是线性表示的系数矩阵)\\\\\\Leftrightarrow {\\color{red}{矩阵B的列向量组}}与矩阵{\\color{red}{A的列向量组}}等价\\\\同理可得\\\\A\\overset{{\\color{red}{r}}}{\\sim}B\\\\\\Leftrightarrow 矩阵{\\color{red}{B的行向量组}}与矩阵{\\color{red}{A的行向量组}}等价.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$向量组B:b_1,\\dots,b_l能由向量组A:a_1,\\dots,a_m线性表示\\\\\\Leftrightarrow 存在矩阵K,使得AK=B\\\\\\Leftrightarrow 矩阵方程AX=B有解\\\\\\Rightarrow R(A)=R(A,B)\\\\\\because R(B)\\leq R(A,B)\\\\\\therefore R(B)\\leq R(A)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**$向量组A:a_1,\\dots,a_m及B:b_1,\\dots,b_l等价的充要条件是R(A)=R(B)=R(A,B)$.\n",
    "\n",
    "**证明:**\n",
    ">$向量组A和B等价\\\\\\Leftrightarrow \\begin{cases}向量组B能由向量组A线性表示\\Leftrightarrow R(A)=R(A,B)\\\\向量组A能由向量组B线性表示\\Leftrightarrow R(B)=R(A,B)\\end{cases}\\\\从而有R(A)=R(B)=R(A,B)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**总结:**\n",
    "> * $向量b能由向量组A线性表示\\Leftrightarrow 线性方程组Ax=b有解\\Leftrightarrow R(A)=R(A,b).$\n",
    "* $向量组B能由向量组A线性表示\\Leftrightarrow 矩阵方程组AX=B有解\\Leftrightarrow R(A)=R(A,B)\\Rightarrow R(B)\\leq R(A).$\n",
    "* $向量组A与向量组B等价\\Leftrightarrow R(A)=R(B)=R(A,B).$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**知识结构:**\n",
    "> $n维向量\\rightarrow 向量组\\rightarrow\\begin{cases}向量组与矩阵的对应\\\\向量组的线性组合\\rightarrow向量组的线性表示\\rightarrow\\begin{cases}判定定理及必要条件\\\\向量组的等价\\rightarrow 判定定理\\end{cases}\\end{cases}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.向量组的线性相关性\n",
    "## 2.1.回顾-向量组的线性组合\n",
    "**定义:**给定向量组$A:a_1,\\dots,a_m,对于任何一组实数k_1,\\dots,k_m,表达式:k_1a_1,\\dots,k_ma_m$,称为向量组$A$的一个**线性组合**.$k_1,\\dots,k_m$称为这个**线性组合的系数**.\n",
    "\n",
    "**定义:**给定向量组$A:a_1,\\dots,a_m和向量b,如果存在一组实数k_1,\\dots,k_m,使得b=k_1a_1,\\dots,k_ma_m$,则向量$b$是向量组$A$的线性组合,这时称**向量$b$能由向量组$A$的线性表示**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2.问题\n",
    "**问题1:**给定向量组$A$,零向量是否可以由向量组A线性表示?$\\Leftrightarrow$齐次线性方程组$Ax=0$是否存在解?\n",
    "\n",
    "**回答:**齐次线性方程组$Ax=0$一定存在解.事实上,可令$k_1=\\dots k_m=0,则k_1a_1+\\dots +k_ma_m=0(零向量).$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题2:**如果零向量可以由向量组$A$线性表示,线性组合的系数是否不全为零?$\\Leftrightarrow$齐次线性方程组$Ax=0$是否存在非零解?\n",
    "\n",
    "**回答:**齐次线性方程组不一定有非零解,从而线性组合的系数不一定全等于零."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3.向量组的线性相关性\n",
    "**定义:**给定向量组$A:a_1,\\dots,a_m,$如果存在不全为零的实数$k_1,\\dots,k_m$,使得$k_1a_1+\\dots +k_ma_m=0(零向量).$则称向量组$A$是线性相关的,否则称他是线性无关的.\n",
    "\n",
    "$\\therefore 向量组A:a_1,\\dots,a_m线性相关\\Leftrightarrow m元齐次线性方程组Ax=0有非零解\\Leftrightarrow R(A)<m.$\n",
    "\n",
    "**注:**\n",
    ">* 给定的向量组$A$,不是线性相关,就是线性无关,两者必居其一.\n",
    "* 向量组$A:a_1,\\dots,a_m线性相关$,通常是指$m\\geq 2$的情形.\n",
    "* 若向量组只包含一个向量:当$a$是零向量时,线性相关;当$a$不是零向量时,线性无关.\n",
    "* 向量组$A:a_1,\\dots,a_m(m\\geq 2)线性相关,也就是向量组A中,至少有一个向量能由其余m-1个向量线性表示.$\n",
    "* 特别地,\n",
    "    * $a_1,a_2$线性相关当且仅当$a_1,a_2$的分量对应成比例,其几何意义是两向量共线.\n",
    "    * $a_1,a_2,a_3$线性相关的几何意义是三个向量共面."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**总结**:\n",
    "\n",
    "**向量组线性相关的判定:**\n",
    ">$向量组A:a_1,\\dots,a_m线性相关\\\\\\Leftrightarrow 存在不全为零的实数k_1,\\dots,k_m,使得k_1a_1+\\dots +k_ma_m=0(零向量).\\\\\\Leftrightarrow m元齐次线性方程组Ax=0有非零解.\\\\\\Leftrightarrow 矩阵A=(a_1,\\dots,a_m)的秩小于向量的个数m.\\\\\\Leftrightarrow 向量组A中至少有一个向量能由其余m-1个向量线性表示.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**\n",
    ">* 若向量组$A:a_1,\\dots,a_m$线性相关,则向量组$B:a_1,\\dots,{\\color{red}{a_{m+1}}}$也线性相关.\n",
    "* $m个n$维向量组成的向量组,当维数$n$小于向量个数$m$时,一定线性相关.特别地,$n+1个n$维向量一定线性相关.\n",
    "* $设向量组A:a_1,\\dots,a_m$线性无关,而向量组$B:a_1,\\dots,a_m,{\\color{red}{b}}$线性相关,则向量$b$必能有向量$A$线性表示,且表示式是唯一的."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.向量的秩\n",
    "**回顾:**\n",
    ">$\\large 矩阵\\overset{有限向量组与矩阵一一对应}{\\rightleftharpoons}有限向量组.\\\\\\large 矩阵\\overset{系数矩阵,增广矩阵}{\\rightleftharpoons}线性方程组.\\\\\\large 有限向量组\\overset{Ax=b有解,当且仅当\\\\向量b可由矩阵A的列向量组线性表示}{\\rightleftharpoons}线性方程组.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|$n元线性方程组Ax=b,其中A\\in\\mathbb R^{n\\times m}$|$矩阵(A,b)$|$向量组A:a_1,\\dots,a_n及向量b$|\n",
    "|-|-|-|\n",
    "|是否存在解？|$R(A)=R(A,b)成立?$|$向量b能否由向量组A线性表示?$|\n",
    "|无解|$R(A)\\leq R(A,b)$|NO|\n",
    "|有解|$R(A)=R(A,b)$|YES($x$的分量是线性组合的系数)|\n",
    "|有唯一解|$R(A)=R(A,b)=n$|表达式唯一|\n",
    "|有无穷解|$R(A)=R(A,b)<n$|表达式不唯一|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**回顾:**\n",
    "\n",
    "**矩阵的秩的概念**\n",
    "\n",
    "**定义:**在$m\\times n矩阵A$中,任取$k行k列(k\\leq m,n)$,位于这些行列交叉处的$k^2$个元素,不改变它们在$A$中所处的位置次序而得的$k$阶行列式,称为矩阵$A$的**$k$阶子式**.\n",
    "\n",
    "**定义:**设矩阵$A$中有一个不等于零的$r$阶子式$D$,且所有$r+1$阶子式(如果存在的话)全等于零,那么$D$称为矩阵$A$的**最高阶非零子式**,数$r$称为**矩阵$A$的秩**,记作$R(A)$.**规定:**零矩阵的秩等于零.\n",
    "\n",
    "**结论:**矩阵的秩=矩阵中最高阶非零子式的阶数=矩阵对应的行阶梯形矩阵的非零行的行数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**向量组的秩的概念:**\n",
    "\n",
    "**定义:**设有向量组$A$,如果在$A$中能选出$r$个向量$a_1,\\dots,a_r$,满足\n",
    "* 向量组$A_0:a_1,\\dots,a_r$线性无关;\n",
    "* 向量组$A$中任意$r+1$个向量(如果$A$中有$r+1$个向量的话)都线性相关;\n",
    "\n",
    "那么称向量组$A_0$是向量组$A$的一个**最大线性无关的向量组**,简称**最大无关组**.最大无关组所含向量个数$r$称为**向量组$A$的秩**,记作**$R_A$**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">$\\large 矩阵\\overset{有限向量组与矩阵一一对应\\\\{\\color{red}{矩阵的秩等于行(列)向量组的秩}}}{\\rightleftharpoons}有限向量组.\\\\\\large 矩阵\\overset{系数矩阵,增广矩阵}{\\rightleftharpoons}线性方程组.\\\\\\large 有限向量组\\overset{Ax=b有解,当且仅当\\\\向量b可由矩阵A的列向量组线性表示}{\\rightleftharpoons}线性方程组.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**一般地:**\n",
    ">* 向量组$a_1,\\dots,a_m$的秩也记作$R(a_1,\\dots,a_m)$.\n",
    "* 若$D_r$是矩阵$A$的一个最高阶非零子式,则$D_r$所在的$r$列是$A$的列向量组的一个最大无关组,$D_r$所在的$r$行是$A$的行向量组的一个最大无关组.\n",
    "* 向量组的最大无关组一般是不唯一的."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**最大无关组的等价定义**\n",
    "\n",
    "**推论:**向量组$A$和它自己的最大无关组$A_0$是等价的.\n",
    "\n",
    "**推论:**向量组$A$中任意一个向量都能由向量组$A_0$线性表示.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">$\\large 矩阵\\overset{有限向量组与矩阵一一对应\\\\{\\color{red}{矩阵的秩等于行(列)向量组的秩}}}{\\rightleftharpoons}有限向量组.\\\\\\large 矩阵\\overset{系数矩阵,增广矩阵}{\\rightleftharpoons}线性方程组.\\\\\\large 有限向量组\\overset{Ax=b有解,当且仅当\\\\向量b可由矩阵A的列向量组线性表示}{\\rightleftharpoons}线性方程组.\\\\\\large {\\color{red}{有限向量组\\overset{向量组与自己的最大无关组等价}{\\rightleftharpoons}无限向量组}}.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**最大无关组的意义**\n",
    "\n",
    "**结论:**向量组$A$和它自己的最大无关组$A_0$是等价的.\n",
    "\n",
    "* 用$A_0$来代表$A$,掌握了最大无关组,就掌握了向量组的全体.特别,当向量组$A$为无限向量组,就能用有限向量组来代表.\n",
    "* 凡是对有限向量组成立的结论,用最大无关组做过渡,立即可推广到无限向量组的情形中去."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.线性方程组的解的结构\n",
    "**回顾:线性方程组的解的判定**\n",
    ">1. 包含$n$个未知数的齐次线性方程组$Ax=0$有非零解的充要条件是系数矩阵的秩$R(A)<n$.\n",
    "2. 包含$n$个未知数的非齐次线性方程组$Ax=b$有解的充要条件是系数矩阵的秩$R(A)=R(A,b)$,并且\n",
    "    * 当$R(A)=R(A,b)=n$时,方程组有唯一解;\n",
    "    * 当$R(A)=R(A,b)<n$时,方程组有无限多个解."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题:**什么是线性方程组的解的结构?\n",
    "\n",
    "**回答:**所谓线性方程组的解的结构,就是当线性方程组有无限多个解时,解与解之间的相互关系.\n",
    "\n",
    "**备注:**\n",
    ">* 当方程组存在唯一解时,无须讨论解的结构.\n",
    "* 讨论线性方程组的解的结构，一般假设线性方程组有解."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解向量的定义**\n",
    "\n",
    "**定义:**设有齐次线性方程组$Ax=0$,如果$x_1=z_{11},\\dots,x_n=z_{n1}$为该方程组的解,则$\\begin{bmatrix}z_{11}\\\\\\vdots\\\\z_{n1}\\end{bmatrix}$称为方程组的解向量."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**齐次线性方程组的解的性质**\n",
    "\n",
    "**性质1:**若$x=z_1,x=z_2$是齐次线性方程组$Ax=0$的解,则$x=z_1+z_2还是Ax=0$的解.\n",
    "\n",
    "**证明:**$A(z_1+z_2)=Az_1+Az_2=0+0=0.$\n",
    "\n",
    "**性质2:**若$x=z$是齐次线性方程组$Ax=0$的解,$k$为实数,则$x=kz还是Ax=0的解.$\n",
    "\n",
    "**证明:**$A(kz)=k(Az)=k\\times 0=0.$\n",
    "\n",
    "**结论:**若$x=z_1,\\dots,x=z_l$是齐次线性方程组$Ax=0$的解,则$x=k_1z_1+\\dots+k_lz_l$还是$Ax=0$的解.\n",
    "\n",
    "**结论:**\n",
    "> * 已知齐次线性方程组$Ax=0$的几个解向量,可以通过这些解向量的线性组合给出更多的解.\n",
    "* 能否通过有限个解向量的线性组合把$Ax=0$的解全部表示出来?可以！\n",
    "* 把$Ax=0$的全体解组成的集合记作$S$,若求得$S$的一个最大无关组$S_0:x=z_1,\\dots,x=z_t$,那么$Ax=0$的通解可表示为$x=k_1z_1+\\dots +k_tz_t$.\n",
    "* 齐次线性方程组的解集的最大无关组称为该齐次线性方程组的**基础解系**(不唯一)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**基础解系的概念**\n",
    "\n",
    "**定义:**齐次线性方程$Ax=0$的一组解向量:$z_1,\\dots,z_r$,如果满足\n",
    "* $z_1,\\dots,z_r$线性无关;\n",
    "* 方程组中任意一个解都可以表示为$z_1,\\dots,z_r$的线性组合,\n",
    "\n",
    "那么称这组解是齐次线性方程组的一个**基础解系**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**设$m\\times n矩阵的秩R(A)=r,则n元齐次线性方程组Ax=0的解集S的秩R_S=n-r.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**非齐次线性方程组的解的性质**\n",
    "\n",
    "**性质3:**若$x=w_1,x=w_2$是非齐次线性方程组$Ax=b$的解,则$x=w_1-w_2$是对应的齐次线性方程组$Ax=0$(可推导出)的解.\n",
    "\n",
    "**证明:**$A(w_1-w_2)=Aw_1-Aw_2=b-b=0.$\n",
    "\n",
    "**性质4:**若$x=w$是非齐次线性方程组$Ax=b$的解,$x=z$是导出组$Ax=0$的解,则$x=z+w还是Ax=b$的解.\n",
    "\n",
    "**证明:**$A(z+w)=Az+Aw=0+b=b$.\n",
    "\n",
    "**根据性质3和4可知**\n",
    ">* 若$x=w^*是Ax=b的解,x=z是Ax=0$的解,那么$x=z+w^*也是Ax=b$的解.\n",
    "* 设$Ax=0$的通解为$z=c_1z_1+\\dots+c_{n-r}z_{n-r}$.于是$Ax=b$的通解为$w=c_1z_1+\\dots+c_{n-r}z_{n-r}+w^*$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**小结:**关于线性方程组\n",
    ">* 求解线性方程组(利用矩阵的初等行变换)\n",
    "* 线性方程组的集合意义(四种等价形式)\n",
    "    * 齐次线性方程组的通解能由它的基础解系来构造.\n",
    "        * 基础解系是解集$S$的最大无关组.\n",
    "        * 解集$S$是基础解系的所有可能的线性组合.\n",
    "    * 非齐次线性方程组的通解与其导出组的基础解系的关系."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.向量空间\n",
    "## 5.1.封闭的概念\n",
    "**定义:**所谓<font color=red>封闭</font>,是指集合中任意两个元素作某一运算得到的结果仍属于该集合.\n",
    "\n",
    "**例如:**实数集$\\mathbb R$对四则运算的结果仍属于实数集$\\mathbb R$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.2.向量空间的概念\n",
    "**定义:**设$V是n$维向量的集合,如果\n",
    "1. 集合$V$非空,\n",
    "2. 集合$V$对于向量的加法和乘数两种运算封闭,\n",
    "\n",
    "具体地说,就是:\n",
    "* 若$a\\in V,b\\in V,则a+b\\in V$(对加法封闭).\n",
    "* 若$a\\in V,\\lambda\\in\\mathbb R,则\\lambda a\\in V$(对乘数封闭).\n",
    "\n",
    "那么就称集合$V$为${\\color{red}{向量空间}}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**下列哪些向量组构成向量空间?\n",
    ">1. $n$维实数向量的全体$\\mathbb R^n$.构成向量空间.\n",
    "2. 集合$V_1=\\{{({\\color{red}{0}},x_2,\\dots,x_n)}^T|x_2,\\dots,x_n\\in\\mathbb R\\}$.构成向量空间\n",
    "3. 集合$V_1=\\{{({\\color{red}{1}},x_2,\\dots,x_n)}^T|x_2,\\dots,x_n\\in\\mathbb R\\}$.${\\color{red}{不构成向量空间}}$,对加法和乘数两种运算不封闭.\n",
    "4. 齐次线性方程组的解集$S_1=\\{x|Ax=0\\}$.构成向量空间,由齐次线性方程组的解的性质可知.\n",
    "4. 非齐次线性方程组的解集$S_2=\\{x|Ax=b\\}$.${\\color{red}{不构成向量空间}}$,$\\because$不具备齐次线性方程组的解的性质."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**齐次线性方程组的解集称为齐次线性方程组的${\\color{red}{解空间}}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**$a,b$为两个已知的$n$维向量,集合$L=\\{\\lambda a+\\mu b|\\lambda,\\mu\\in\\mathbb{R}\\}$是一个向量空间吗？\n",
    "\n",
    "**解:**设$x_1,x_2\\in L,k\\in\\mathbb{R}$,<br>\n",
    "$\\because x_1+x_2=(\\lambda_1a+\\mu_1b)+(\\lambda_2a+\\mu_2b)\\\\=(\\lambda_1+\\lambda_2)a+(\\mu_1+\\mu_2)b\\in L(对加法运算封闭)\\\\又\\because kx_1=k(\\lambda_1a+\\mu_1b)=(k\\lambda_1)a+(k\\mu_1)b\\in L(对乘法运算封闭)\\\\\\therefore L是一个向量空间.$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**把集合$L=\\{\\lambda a+\\mu b|\\lambda,\\mu\\in\\mathbb{R}\\}$称为${\\color{red}{由向量a,b所生成的向量空间.}}$<br>$一般地,把集合L=\\{\\lambda_1a_1+\\dots+\\lambda_ma_m|\\lambda_1,\\dots,\\lambda_m\\in\\mathbb{R}\\}$称为<font color=red>由向量$a_1,\\dots,a_m$所生成的向量空间.</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设向量组$a_1,\\dots,a_m$和$b_1,\\dots,b_s$等价,记<br>\n",
    "$L_1=\\{\\lambda_1a_1+\\dots+\\lambda_ma_m|\\lambda_1,\\dots,\\lambda_m\\in R\\}$,<br>\n",
    "$L_2=\\{\\mu_1b_1+\\dots+\\mu_sb_s|\\mu_1,\\dots,\\mu_s\\in R\\}$,<br>\n",
    "证$L_1=L_2$.\n",
    "\n",
    "**证:**\n",
    "$\\because (a_1,\\dots,a_m)\\sim (b_1,\\dots,b_s)\\\\\\therefore b_i=\\lambda_{1i}a_1+\\dots+\\lambda_{mi}a_m,(\\lambda_{1i},\\dots,\\lambda_{mi}\\in R,i=1,\\dots,m)\\\\\\therefore L_2=\\{\\mu_1(\\lambda_{11}a_1+\\dots+\\lambda_{m1}a_m)+\\dots+\\mu_s(\\lambda_{1s}a_1+\\dots+\\lambda_{ms}a_m)|\\mu_1,\\dots,\\mu_s\\in R\\}\\\\\\therefore L_2=\\{(\\mu_1\\lambda_{11}+\\dots+\\mu_s\\lambda_{1s})a_1+\\dots+(\\mu_1\\lambda_{m1}+\\dots+\\mu_s\\lambda_{ms})a_m|\\}\\\\\\because \\mu_1\\lambda_{i1}+\\dots+\\mu_s\\lambda_{is}\\in \\mathbb R,i=1,\\dots,m\\\\\\therefore L_1=L_2.$\n",
    "\n",
    "**推论:**等价的向量组所生成的空间相等."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3.向量空间的基的概念\n",
    "**定义:**设有向量空间$V$,如果在$V$中能选出$r$个向量$a_1,\\dots,a_r$,满足\n",
    "* $a_1,\\dots,a_r$线性无关;\n",
    "* $V$中任意一个向量都能由$a_1,\\dots,a_r$线性表示;\n",
    "\n",
    "那么称向量组$a_1,\\dots,a_r$是向量空间$V$的一个基.$r$称为向量空间$V$的维数,并称$V为r$维向量空间."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* $\\large 向量空间\\overset{对应}{\\Leftrightarrow}向量组\\\\\\large向量空间的基\\overset{对应}{\\Leftrightarrow}向量组的最大无关组\\\\\\large向量空间的维数\\overset{对应}{\\Leftrightarrow}向量组的秩$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**求如下向量空间的基?\n",
    ">1. $n$维向量的全体$\\mathbb R^n.E_n$的列向量组是$\\mathbb R^n$的一个基,故$\\mathbb R^n$维数是$n$.\n",
    "2. 集合$V_1=\\{{({\\color{red}{0}},x_2,\\dots,x_n)}^T|x_2,\\dots,x_n\\in\\mathbb R\\}$.$E_n$的后$n-1$个列向量是$V_1$的一个基,故$V_1$的维数等于$n-1$.\n",
    "3. $n$元齐次线性方程组的解集$S_1=\\{x|Ax=0\\}$.齐次线性方程组的基础解系是$S_1$的一个基,故$S_1$的维数等于$n-R(A)$.\n",
    "4. 由$a_1,\\dots,a_m$所生成的向量空间$L=\\{\\lambda_1a_1+\\dots+\\lambda_ma_m|\\lambda_1,\\dots,\\lambda_m\\in\\mathbb{R}\\}$\n",
    ">  * 若$a_1,\\dots,a_m$线性无关,则$a_1,\\dots,a_m$是向量空间$L$的一个基.\n",
    " * 若$a_1,\\dots,a_m$线性相关,则向量组$A:a_1,\\dots,a_m$等价于向量组$A$的最大无关组$A_0:a_1,\\dots,a_r,$<br>\n",
    "$\\Rightarrow L=L_1=\\{\\lambda_1a_1+\\dots+\\lambda_ra_r|\\lambda_1,\\dots,\\lambda_r\\in\\mathbb{R}\\}$<br>\n",
    "故向量组$A_0$就是$L$的一个基,$A_0$中向量的个数就是$L$的维数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**如果在向量空间$V$中取定一个基$a_1,\\dots,a_r$,那么$V$中任意一个向量可唯一表示为$x=\\lambda_1a_1+\\dots+\\lambda_ra_r$<br>\n",
    "数组$\\lambda_1,\\dots,\\lambda_r$称为向量$x$在基$a_1,\\dots,a_r$中的坐标."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**$b$向量在基($E_n$的向量组)中的坐标为$x$向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]\\end{matrix}\\right] = \\left[\\begin{matrix}\\left[\\begin{matrix}x_{0}\\\\x_{1}\\\\x_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡b_{0}⎤⎤   ⎡⎡x_{0}⎤⎤\n",
       "⎢⎢     ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎢⎢b_{1}⎥⎥ = ⎢⎢x_{1}⎥⎥\n",
       "⎢⎢     ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎣⎣b_{2}⎦⎦   ⎣⎣x_{2}⎦⎦"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xe=sp.eye(xn)\n",
    "xs=[sp.Matrix([[j[:,i]] for i in range(xn)]).T for j in [xe]]\n",
    "xes=xs[0]\n",
    "\n",
    "xbs=sp.Matrix([[getM('b',xn,1),]])\n",
    "xbs\n",
    "\n",
    "xx,=(getM(i,xn,1) for i in 'x')\n",
    "xx,xbs,xes\n",
    "sp.Eq(xbs,xes*xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**上三角矩阵的列向量组也是$\\mathbb R^3$的一个基"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]\\end{matrix}\\right] = \\left[\\begin{matrix}\\left[\\begin{matrix}x_{0} + x_{1} + x_{2}\\\\x_{1} + x_{2}\\\\x_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡b_{0}⎤⎤   ⎡⎡x_{0} + x_{1} + x_{2}⎤⎤\n",
       "⎢⎢     ⎥⎥   ⎢⎢                     ⎥⎥\n",
       "⎢⎢b_{1}⎥⎥ = ⎢⎢    x_{1} + x_{2}    ⎥⎥\n",
       "⎢⎢     ⎥⎥   ⎢⎢                     ⎥⎥\n",
       "⎣⎣b_{2}⎦⎦   ⎣⎣        x_{2}        ⎦⎦"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xe=sp.Matrix([[1,1,1],[0,1,1],[0,0,1]])\n",
    "xs=[sp.Matrix([[j[:,i]] for i in range(xn)]).T for j in [xe]]\n",
    "xes=xs[0]\n",
    "\n",
    "xbs=sp.Matrix([[getM('b',xn,1),]])\n",
    "xbs\n",
    "\n",
    "xx,=(getM(i,xn,1) for i in 'x')\n",
    "xx,xbs,xes\n",
    "sp.Eq(xbs,xes*xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**结论:**同一向量在不同基中的坐标是不同的."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}\\left[\\begin{matrix}2\\\\2\\\\-1\\end{matrix}\\right] & \\left[\\begin{matrix}2\\\\-1\\\\2\\end{matrix}\\right] & \\left[\\begin{matrix}-1\\\\2\\\\2\\end{matrix}\\right]\\end{matrix}\\right], \\quad \\left[\\begin{matrix}\\left[\\begin{matrix}1\\\\0\\\\-4\\end{matrix}\\right] & \\left[\\begin{matrix}4\\\\3\\\\2\\end{matrix}\\right]\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡⎡2 ⎤  ⎡2 ⎤  ⎡-1⎤⎤  ⎡⎡1 ⎤  ⎡4⎤⎤⎞\n",
       "⎜⎢⎢  ⎥  ⎢  ⎥  ⎢  ⎥⎥  ⎢⎢  ⎥  ⎢ ⎥⎥⎟\n",
       "⎜⎢⎢2 ⎥  ⎢-1⎥  ⎢2 ⎥⎥, ⎢⎢0 ⎥  ⎢3⎥⎥⎟\n",
       "⎜⎢⎢  ⎥  ⎢  ⎥  ⎢  ⎥⎥  ⎢⎢  ⎥  ⎢ ⎥⎥⎟\n",
       "⎝⎣⎣-1⎦  ⎣2 ⎦  ⎣2 ⎦⎦  ⎣⎣-4⎦  ⎣2⎦⎦⎠"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xa=sp.Matrix([[2,2,-1],[2,-1,2],[-1,2,2]])#A矩阵\n",
    "xb=sp.Matrix([[1,4],[0,3],[-4,2]])#A矩阵\n",
    "# A、B矩阵的向量组形式\n",
    "xas,xbs=[sp.Matrix([[j[:,i]] for i in range(j.shape[1])]).T for j in [xa,xb]]\n",
    "xas,xbs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证$a_1,a_2,a_3$是$\\mathbb R^3$的一个基,并求$b_1,b_2$在这个基中的坐标."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解:**<br>\n",
    "**分析:**\n",
    "> * $a_1,a_2,a_3$是$\\mathbb R^3$的一个基$\\Leftrightarrow R(a_1,a_2,a_3)=3$.\n",
    "* $b_1,b_2$在这个基中的坐标$\\Leftrightarrow 用a_1,a_2,a_3表示b_1,b_2$.\n",
    "* 当$A\\overset{r}{\\sim}B$,时，$A$的列向量组与$B$的列向量组有相同的线性关系.\n",
    "\n",
    "为此,考虑把$(A,B)=(a_1,a_2,a_3,b_1,b_2)$化为${\\color{red}{行最简形矩阵}}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 2 & -1 & 1 & 4\\\\2 & -1 & 2 & 0 & 3\\\\-1 & 2 & 2 & -4 & 2\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2   2   -1  1   4⎤\n",
       "⎢                 ⎥\n",
       "⎢2   -1  2   0   3⎥\n",
       "⎢                 ⎥\n",
       "⎣-1  2   2   -4  2⎦"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (A,B)\n",
    "xab=sp.Matrix.row_join(xa,xb)\n",
    "xab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$(A,B)\\sim$行最简形矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 2 & -1 & 1 & 4\\\\0 & -3 & 3 & -1 & -1\\\\0 & 3.0 & 1.5 & -3.5 & 4.0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2   2   -1    1     4 ⎤\n",
       "⎢                      ⎥\n",
       "⎢0  -3    3    -1   -1 ⎥\n",
       "⎢                      ⎥\n",
       "⎣0  3.0  1.5  -3.5  4.0⎦"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# +乘k\n",
    "xp,xq,xk=1,0,-1\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=2,0,1/2\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 2 & -1 & 1 & 4\\\\0 & -3 & 3 & -1 & -1\\\\0 & 0 & 4.5 & -4.5 & 3.0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2  2   -1    1     4 ⎤\n",
       "⎢                     ⎥\n",
       "⎢0  -3   3    -1   -1 ⎥\n",
       "⎢                     ⎥\n",
       "⎣0  0   4.5  -4.5  3.0⎦"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# +乘k\n",
    "xp,xq,xk=2,1,1\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 2 & 0 & 0 & 4.66666666666667\\\\0 & -3 & 0 & 2.0 & -3.0\\\\0 & 0 & 4.5 & -4.5 & 3.0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2  2    0    0    4.66666666666667⎤\n",
       "⎢                                  ⎥\n",
       "⎢0  -3   0   2.0         -3.0      ⎥\n",
       "⎢                                  ⎥\n",
       "⎣0  0   4.5  -4.5        3.0       ⎦"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# +乘k\n",
    "xp,xq,xk=0,2,1/4.5\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=1,2,-3/4.5\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 0 & 0 & 1.33333333333333 & 2.66666666666667\\\\0 & -3 & 0 & 2.0 & -3.0\\\\0 & 0 & 4.5 & -4.5 & 3.0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2  0    0   1.33333333333333  2.66666666666667⎤\n",
       "⎢                                              ⎥\n",
       "⎢0  -3   0         2.0               -3.0      ⎥\n",
       "⎢                                              ⎥\n",
       "⎣0  0   4.5        -4.5              3.0       ⎦"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# +乘k\n",
    "xp,xq,xk=0,1,2/3\n",
    "xab[xp,:]=xab[xp,:]+xab[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1.0 & 0 & 0 & 0.666666666666667 & 1.33333333333333\\\\0 & 1.0 & 0 & -0.666666666666667 & 1.0\\\\0 & 0 & 1.0 & -1.0 & 0.666666666666667\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1.0   0    0   0.666666666666667   1.33333333333333 ⎤\n",
       "⎢                                                    ⎥\n",
       "⎢ 0   1.0   0   -0.666666666666667         1.0       ⎥\n",
       "⎢                                                    ⎥\n",
       "⎣ 0    0   1.0         -1.0         0.666666666666667⎦"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 乘k\n",
    "xp,xk=0,1/2\n",
    "xab[xp,:]=xab[xp,:]*xk\n",
    "\n",
    "# 乘k\n",
    "xp,xk=1,-1/3\n",
    "xab[xp,:]=xab[xp,:]*xk\n",
    "\n",
    "# 乘k\n",
    "xp,xk=2,1/4.5\n",
    "xab[xp,:]=xab[xp,:]*xk\n",
    "\n",
    "# show\n",
    "xab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**由上可得:**<br>\n",
    "$b_1,b_2$在这个基中的坐标如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}0.666666666666667\\\\-0.666666666666667\\\\-1.0\\end{matrix}\\right], \\quad \\left[\\begin{matrix}1.33333333333333\\\\1.0\\\\0.666666666666667\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡0.666666666666667 ⎤  ⎡1.33333333333333 ⎤⎞\n",
       "⎜⎢                  ⎥  ⎢                 ⎥⎟\n",
       "⎜⎢-0.666666666666667⎥, ⎢       1.0       ⎥⎟\n",
       "⎜⎢                  ⎥  ⎢                 ⎥⎟\n",
       "⎝⎣       -1.0       ⎦  ⎣0.666666666666667⎦⎠"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xab[:,3],xab[:,4]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
